package com.example.q_kang.hhh.order.match;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.q_kang.hhh.R;
import com.example.q_kang.hhh.bean.ImgBean;
import com.hjq.toast.ToastUtils;
import com.previewlibrary.GPreviewBuilder;
import com.previewlibrary.enitity.IThumbViewInfo;
import com.sskj.base.MVPActivity;
import com.sskj.base.adapter.slimadapter.IViewInjector;
import com.sskj.base.adapter.slimadapter.SlimAdapter;
import com.sskj.base.adapter.slimadapter.SlimInjector;
import com.sskj.base.base.App;
import com.sskj.base.bean.NoteListBean;
import com.sskj.base.bean.SellDetailBean;
import com.sskj.base.utils.ClickUtil;
import com.sskj.base.utils.CopyUtil;
import com.sskj.base.utils.DateUtil;
import com.sskj.base.utils.ImageUtil;
import com.sskj.base.view.ToolBarLayout;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * @author Hey
 * Create at  2019/04/13
 */
public class BuyConfirmActivity extends MVPActivity<BuyConfirmPresenter> {
    @BindView(R.id.toolbar)
    ToolBarLayout toolBar;
    @BindView(R.id.tv_match_money)
    TextView tvMatchMoney;
    @BindView(R.id.tv_create_date)
    TextView tvCreateDate;
    @BindView(R.id.tv_count_down)
    TextView tvCountDown;
    @BindView(R.id.tv_accept_account)
    TextView tvAcceptAccount;
    @BindView(R.id.tv_accept_name)
    TextView tvAcceptName;
    @BindView(R.id.tv_accept_mobile)
    TextView tvAcceptMobile;
    @BindView(R.id.tv_copy_mobile)
    TextView tvCopyMobile;
    @BindView(R.id.tv_accept_tuijian)
    TextView tvAcceptTuijian;
    @BindView(R.id.tv_accept_beizhu)
    TextView tvAcceptBeizhu;
    @BindView(R.id.tv_alipay_account)
    TextView tvAlipayAccount;
    @BindView(R.id.tv_copy_alipay)
    TextView tvCopyAlipay;
    @BindView(R.id.ll_alipay)
    LinearLayout llAlipay;
    @BindView(R.id.tv_alipay_wechat)
    TextView tvAlipayWechat;
    @BindView(R.id.tv_copy_wechat)
    TextView tvCopyWechat;
    @BindView(R.id.iv_wechat)
    ImageView ivWechat;
    @BindView(R.id.ll_wechat)
    LinearLayout llWechat;
    @BindView(R.id.tv_bank_open)
    TextView tvBankOpen;
    @BindView(R.id.tv_bank_card)
    TextView tvBankCard;
    @BindView(R.id.tv_copy_bank)
    TextView tvCopyBank;
    @BindView(R.id.tv_bank_owner)
    TextView tvBankOwner;
    @BindView(R.id.ll_bank)
    LinearLayout llBank;
    @BindView(R.id.iv_pay_img)
    ImageView ivPayImg;
    @BindView(R.id.et_liuyan)
    EditText etLiuyan;
    @BindView(R.id.btn_liuyan)
    Button btnLiuyan;
    @BindView(R.id.ll_shoukuan_count_down)
    LinearLayout ll_shoukuan_count_down;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;

    private SlimAdapter<NoteListBean> slimAdapter;
    private String order;
    private String order_type;
    private String leaveMsgId;
    private List<ImgBean> localMedia = new ArrayList<>();
    private List<ImgBean> weChatMedia = new ArrayList<>();

    @Override
    public int getLayoutId() {
        return R.layout.activity_buy_confirm;
    }

    @Override
    public BuyConfirmPresenter getPresenter() {
        return new BuyConfirmPresenter();
    }

    @Override
    public void initView() {
        order = getIntent().getStringExtra("order");
        order_type = getIntent().getStringExtra("order_type");
        if (order_type.equals("0")) {
            ll_shoukuan_count_down.setVisibility(View.VISIBLE);
        } else if (order_type.equals("1")) {
            ll_shoukuan_count_down.setVisibility(View.GONE);
        }
        recyclerView.setNestedScrollingEnabled(false);
        toolBar.setLeftButtonOnClickListener(view -> finish());
        mPresenter.getOrderDetail(order);
        ClickUtil.click(300, tvCopyWechat, view -> {
            CopyUtil.copy(this, tvAlipayWechat.getText().toString());
        });
        ClickUtil.click(300, tvCopyMobile, view -> {
            CopyUtil.copy(this, tvAcceptMobile.getText().toString());
        });
        ClickUtil.click(300, tvCopyBank, view -> {
            CopyUtil.copy(this, tvBankCard.getText().toString());
        });
        ClickUtil.click(300, tvCopyAlipay, view -> {
            CopyUtil.copy(this, tvAlipayAccount.getText().toString());
        });
        ClickUtil.click(300, ivPayImg, view -> {
            GPreviewBuilder.from(this)//activity实例必须
                    .setData(localMedia)//集合
                    .setCurrentIndex(0)
                    .setSingleFling(false)//是否在黑屏区域点击返回
                    .setDrag(false)//是否禁用图片拖拽返回
                    .setType(GPreviewBuilder.IndicatorType.Dot)//指示器类型
                    .start();//启动
        });
        ClickUtil.click(300, ivWechat, view -> {
            GPreviewBuilder.from(this)//activity实例必须
                    .setData(weChatMedia)//集合
                    .setCurrentIndex(0)
                    .setSingleFling(false)//是否在黑屏区域点击返回
                    .setDrag(false)//是否禁用图片拖拽返回
                    .setType(GPreviewBuilder.IndicatorType.Dot)//指示器类型
                    .start();//启动
        });
        ClickUtil.click(300, btnLiuyan, view -> {
            if (TextUtils.isEmpty(getEtext(etLiuyan))) {
                ToastUtils.show(App.INSTANCE.getString(R.string.appJavaBuyConfirmActivity5));
                return;
            }
            if (TextUtils.isEmpty(leaveMsgId)) {
                ToastUtils.show(App.INSTANCE.getString(R.string.appJavaBuyConfirmActivity6));
                return;
            }
            mPresenter.leaveMsg(leaveMsgId, getEtext(etLiuyan));
        });
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        slimAdapter = SlimAdapter.create().register(R.layout.item_note, new SlimInjector<NoteListBean>() {
            @Override
            public void onInject(NoteListBean data, IViewInjector injector) {
                injector.text(R.id.tv_name, data.getUsername() + "：")
                        .text(R.id.tv_content, data.getContent());
            }
        }).attachTo(recyclerView).updateData(new ArrayList());
    }

    @Override
    public void initData() {

    }

    public static void start(Context context, String order, String order_type) {
        Intent intent = new Intent(context, BuyConfirmActivity.class);
        intent.putExtra("order", order);
        intent.putExtra("order_type", order_type);
        context.startActivity(intent);
    }

    public void getDataSuccess(SellDetailBean sellDetailBean) {
        slimAdapter.updateData(sellDetailBean.getmInfo());
        //订单信息
        if (sellDetailBean.getInfo() != null) {
            leaveMsgId = sellDetailBean.getInfo().getId();
            tvMatchMoney.setText(sellDetailBean.getInfo().getPd_price());
            if (!TextUtils.isEmpty(sellDetailBean.getInfo().getAdd_time())) {
                tvCreateDate.setText(DateUtil.getDateFormat(Long.valueOf(sellDetailBean.getInfo().getAdd_time()) * 1000, DateUtil.PATTERN_J));
            }
            if (order_type.equals("0")) {
                if (sellDetailBean.getInfo().getSkDate() > 0) {
                    startCountDown((int) (sellDetailBean.getInfo().getSkDate() - System.currentTimeMillis() / 1000));
                } else {
                    tvCountDown.setText("00:00:00");
                }
            }
            localMedia.add(new ImgBean(sellDetailBean.getInfo().getPay_img()));
            ImageUtil.setImage(this, sellDetailBean.getInfo().getPay_img(), ivPayImg);
        }
        //卖方信息
        if (sellDetailBean.getjInfo() != null) {
            tvAcceptAccount.setText(sellDetailBean.getjInfo().getAccount());
            tvAcceptName.setText(sellDetailBean.getjInfo().getNickname());
            tvAcceptMobile.setText(sellDetailBean.getjInfo().getMobile());
            tvAcceptBeizhu.setText(sellDetailBean.getjInfo().getPd_note());
            if (TextUtils.isEmpty(sellDetailBean.getjInfo().getWx_name())) {
                llWechat.setVisibility(View.GONE);
            } else {
                llWechat.setVisibility(View.VISIBLE);
                tvAlipayWechat.setText(sellDetailBean.getjInfo().getWx_name());
                weChatMedia.add(new ImgBean(sellDetailBean.getjInfo().getWx_code()));
                ImageUtil.setImage(App.INSTANCE, sellDetailBean.getjInfo().getWx_code(), ivWechat);
            }
            if (TextUtils.isEmpty(sellDetailBean.getjInfo().getZfb_name())) {
                llAlipay.setVisibility(View.GONE);
            } else {
                llAlipay.setVisibility(View.VISIBLE);
                tvAlipayAccount.setText(sellDetailBean.getjInfo().getZfb_name());
            }
        }
        //卖方推荐人
        if (sellDetailBean.getjTjrInfo() != null) {
            tvAcceptTuijian.setText(sellDetailBean.getjTjrInfo().getUsername());
        } else if (sellDetailBean.getjTjrInfo() == null) {
            tvAcceptTuijian.setText(App.INSTANCE.getString(R.string.kong));
        }
        if (sellDetailBean.getBankInfo() == null) {
            llBank.setVisibility(View.GONE);
        } else {
            llBank.setVisibility(View.VISIBLE);
            tvBankOpen.setText(sellDetailBean.getBankInfo().getName_cn());
            tvBankCard.setText(sellDetailBean.getBankInfo().getBank_account());
            tvBankOwner.setText(sellDetailBean.getBankInfo().getBank_name());
        }
    }

    private long second;
    private Disposable mDisposable;

    private void startCountDown(int lastSeconds) {
        second = lastSeconds;
        Observable.interval(1, 1, TimeUnit.SECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Long>() {

                    @Override
                    public void onSubscribe(Disposable d) {
                        mDisposable = d;
                    }

                    @Override
                    public void onNext(Long aLong) {
                        second--;
                        if (second < 0) {
                            mDisposable.dispose();
//                            Toast.makeText(getActivity(), App.INSTANCE.getString(R.string.appJavaBuyConfirmActivity11), Toast.LENGTH_SHORT).show();
                            return;
                        }
                        tvCountDown.setText(DateUtil.second2TimeSecond(second));
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }


    public void leaveSuccess() {
        mPresenter.getOrderDetail(order);
    }
}
